package com.woshengqile.dao;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.woshengqile.Pageable;
import com.woshengqile.model.VideoInfo;
import com.woshengqile.model.VideoInfoCategory;
import com.woshengqile.model.VideoInfoTag;

/**
 * Dao - 视频
 * @author Think
 */
public class VideoInfoDao extends BaseDao<VideoInfo> {

	public VideoInfoDao() {
		super(VideoInfo.class);
	}
	
	/**
	 * 查找视频分页
	 * @param videoInfoCategory 视频分类
	 * @param videoInfoTag 视频标签
	 * @param pageable 分页信息
	 * @return 分页
	 */
	public Page<VideoInfo> findPage(VideoInfoCategory videoInfoCategory, VideoInfoTag videoInfoTag, Pageable pageable) {
		StringBuilder sqlExceptSelect = new StringBuilder("FROM `video_info` vi WHERE 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		
		if (videoInfoCategory != null) {
			sqlExceptSelect.append(" AND vi.id IN (SELECT t.video_info_id FROM video_info_video_info_category t WHERE t.video_info_category_id = ?)");
			params.add(videoInfoCategory.getId());
		}
		if (videoInfoTag != null) {
			sqlExceptSelect.append(" AND vi.id IN (SELECT t.video_info_id FROM video_info_video_info_tag t WHERE t.video_info_tag_id = ?)");
			params.add(videoInfoTag.getId());
		}
		return super.findPage(sqlExceptSelect.toString(), pageable, params);
	}

}
